/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.tech15.core.dao.SysUserDao;
import com.tech15.core.model.SysUser;
import com.tech15.pub.dao.MsgTipsDao;
import com.tech15.pub.model.MsgTips;
import com.tech15.task.model.*;
import com.tech15.task.dao.*;
import com.tech15.task.service.*;
import com.tech15.task.vo.query.*;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class TaskReturnManager extends BaseManager<TaskReturn,Long>{

	private TaskReturnDao taskReturnDao;
	private SysUserDao sysUserDao;
	private MsgTipsDao msgTipsDao;
	private TaskReturnProcessDao taskReturnProcessDao;
	
	public void setTaskReturnProcessDao(TaskReturnProcessDao taskReturnProcessDao) {
		this.taskReturnProcessDao = taskReturnProcessDao;
	}
	
	public void setMsgTipsDao(MsgTipsDao msgTipsDao) {
		this.msgTipsDao = msgTipsDao;
	}
	public void setSysUserDao(SysUserDao sysUserDao) {
		this.sysUserDao = sysUserDao;
	}
	public void setTaskReturnDao(TaskReturnDao dao) {
		this.taskReturnDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.taskReturnDao;
	}
	
	@Transactional(readOnly=true)
	public TaskReturn getById(Long id){
		TaskReturn tr = taskReturnDao.getById(id);
		if(ObjectUtils.isNotEmpty(tr.getThr())){
			SysUser user = sysUserDao.getById(tr.getThr());
			if(user != null){
				tr.setRealname(user.getRealname());
			}
		}
		
		if(ObjectUtils.isNotEmpty(tr.getSsr())){
			SysUser user = sysUserDao.getById(tr.getSsr());
			if(user != null){
				tr.setSsrxm(user.getRealname());
			}
		}
		if(ObjectUtils.isNotEmpty(tr.getBz())){
			SysUser user = sysUserDao.getById(tr.getBz());
			if(user != null){
				tr.setBzxm(user.getRealname());
			}
		}
		
		if(ObjectUtils.isNotEmpty(tr.getGg())){
			SysUser user = sysUserDao.getById(tr.getGg());
			if(user != null){
				tr.setGgxm(user.getRealname());
			}
		}
		return tr;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(TaskReturnQuery query) {
		return taskReturnDao.findPage(query);
	}
	
	@Transactional(readOnly=true)
	public Page findByPageRequest(PageRequest pr) {
		return taskReturnDao.findByPageRequest(pr);
	}
	
	@Transactional(readOnly=true)
	public Page my(TaskReturnQuery query) {
		return taskReturnDao.my(query);
	}
	public void ss(TaskReturn taskReturn) {
		taskReturn.setZt(-2);
		taskReturn.setSssj(DateTools.getCurrentTime());
		taskReturn.setGbzsj(DateTools.getCurrentTime());
		taskReturn.setCurclr(taskReturn.getBz());
		taskReturnDao.update(taskReturn);
		taskReturnDao.flush();
		
		MsgTips tips = new MsgTips();
		tips.setBt(taskReturn.getSsly());
		tips.setContent(taskReturn.getSsly());
		tips.setTablename(Notes.taskReturn);
		tips.setTableid(taskReturn.getTrid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUserid(taskReturn.getBz());
		tips.setUrl(Notes.taskReturnUrl+"trid="+taskReturn.getTrid());
		msgTipsDao.save(tips);
	}
	public void ss2(TaskReturn taskReturn) {
		taskReturn.setZt(3);
		taskReturn.setGggsj(DateTools.getCurrentTime());
		taskReturn.setCurclr(taskReturn.getGg());
		taskReturnDao.update(taskReturn);
		taskReturnDao.flush();
		
		MsgTips tips = new MsgTips();
		tips.setBt(taskReturn.getSsly());
		tips.setContent(taskReturn.getSsly());
		tips.setTablename(Notes.taskReturn);
		tips.setTableid(taskReturn.getTrid()+"");
		tips.setZt(0);
		tips.setXxsj(DateTools.getCurrentTime());
		tips.setUserid(taskReturn.getGg());
		tips.setUrl(Notes.taskReturnUrl+"trid="+taskReturn.getTrid());
		msgTipsDao.save(tips);
	}
	public Integer myReturnCount(Long userid) {
		return taskReturnDao.myReturnCount(userid);
	}
	public void updateTr(TaskReturn taskReturn) {
		if(ObjectUtils.isEmpty(taskReturn.getGg())){
			/*taskReturnDao.delete("update task_return_process t set t.zt=1 where t.trid="+taskReturn.getTrid()+" and t.userid="+taskReturn.getZrr());
			taskReturnDao.delete("update task_return_process t set t.zt=1,t.clsj=sysdate where t.trid="+taskReturn.getTrid()+" and t.userid="+taskReturn.getZrr());
			*/
			
			List<TaskReturnProcess> list = taskReturnProcessDao.findAllBy("trid", taskReturn.getTrid());
			for(TaskReturnProcess trp : list){
				if(trp.getUserid().longValue() == taskReturn.getZrr().longValue()){
					trp.setClsj(DateTools.getCurrentTime());
					trp.setCljg(taskReturn.getSsjg());
				}
				trp.setZt(1);
				taskReturnProcessDao.update(trp);
			}
			
			taskReturn.setGg(taskReturn.getZrr());
			taskReturnDao.update(taskReturn);
		}else{
			taskReturnDao.update(taskReturn);
		}
	}
	
}
